<template>
  <el-dialog
    :title="!dataForm.chooseTypeId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-form-item label="商品筛选类别名" prop="chooseTypeName" label-width="150px">
        <el-input v-model="dataForm.chooseTypeName" placeholder="商品筛选类别名"></el-input>
      </el-form-item>
     <el-form-item label="状态" size="mini" prop="chooseTypeState" label-width="150px">
        <el-radio-group v-model="dataForm.chooseTypeState">
          <el-radio :label="0">启用</el-radio>
          <el-radio :label="1">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" :disabled="submitVisible" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        submitVisible: false,
        dataForm: {
          chooseTypeId: '',
          chooseTypeName: '',
          chooseTypeState: ''
        },
        dataRule: {
          chooseTypeName: [
            { required: true, message: '商品筛选类别名', trigger: 'blur' }
          ],
          chooseTypeState: [
            { required: true, message: '请设置商品筛选类别状态', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.chooseTypeState = 0
        this.dataForm.chooseTypeId = id || ''
        this.visible = true
        this.submitVisible = false
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
        if (this.dataForm.chooseTypeId) {
          this.$http({
            url: this.$http.adornUrl(`/pig-center-biz/shopset/goodsChooseType/info`),
            method: 'post',
            params: this.$http.adornParams({
              'id': this.dataForm.chooseTypeId
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.chooseTypeName = data.goodsChooseType.chooseTypeName
              this.dataForm.chooseTypeState = data.goodsChooseType.chooseTypeState
            }
          })
        }
      },

      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.submitVisible = true
            this.$http({
              url: this.$http.adornUrl(`/pig-center-biz/shopset/goodsChooseType/${!this.dataForm.chooseTypeId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'chooseTypeId': this.dataForm.chooseTypeId || undefined,
                'chooseTypeName': this.dataForm.chooseTypeName,
                'chooseTypeState': this.dataForm.chooseTypeState
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<style>

</style>
